<template>
  <div class="user_data" v-if="userInfo">
    <div class="data_id">
      <p>管理我的账号</p>
      <div class="data_my">
        <div class="my_info">
          <div class="photo">
            <van-uploader :after-read="afterRead" />
            <!-- <input type="file" @change="Image" /> -->
            <img :src="photo" alt="" />
          </div>
          <div class="info_name">
            <span>{{ userInfo.nickname }}</span>
            <span>绑定手机号：{{ userInfo.phone }}</span>
          </div>
        </div>
        <span class="current">当前账号</span>
      </div>
    </div>
    <div class="data_form">
      <van-field v-model="name" label="昵称" input-align="right" />
      <van-field
        v-model="id"
        label="ID号"
        input-align="right"
        right-icon="lock"
        readonly
      />
      <van-field
        v-model="phone"
        label="手机号码"
        input-align="right"
        right-icon="lock"
        readonly
      />
      <van-field
        v-model="password"
        label="密码"
        input-align="right"
        right-icon="arrow"
        readonly
        @click="changePassword"
      />
    </div>
    <div class="login_operate">
      <van-button type="danger" block @click="SaveChange">保存修改</van-button>
      <van-button block @click="logOut">退出登录</van-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      userInfo: null,
      name: "",
      id: "",
      phone: "",
      password: "点击修改密码",
      photo: null,
    };
  },
  created() {
    this.axios.get("/api/user").then((response) => {
      this.userInfo = response.data.data;
      this.name = this.userInfo.nickname;
      this.id = this.userInfo.uid;
      this.phone = this.userInfo.phone;
      this.photo = this.userInfo.avatar;
    });
  },
  methods: {
    logOut() {
      this.$dialog
        .confirm({
          title: "提示",
          message: "确认退出登录？",
        })
        .then(() => {
          this.axios.get("/api/logout").then((response) => {
            if (response.data.status == 200) {
              localStorage.setItem("tokenData", "");
              this.$store.state.data.token = null;
              this.$store.state.data.userInfo = null;
              this.$router.push("/");
              window.location.reload();
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    },
    SaveChange() {
      this.axios
        .post("/api/user/edit", {
          avatar: this.photo,
          nickname: this.name,
        })
        .then(() => {
          this.$router.push({
            name: "user",
            params: {
              msg: "修改成功",
              flag: true,
            },
          });
        });
    },
    changePassword() {
      this.$router.push("/change_password");
    },
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      var formData = new FormData();
      formData.append("file", file.file);
      this.axios({
        method: "POST",
        url: "/api/upload/image",
        data: formData,
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }).then((response) => {
        this.photo = response.data.data.url;
      });
    },
  },
};
</script>

<style lang="less">
.user_data {
  width: 100%;
  .data_id {
    width: 100%;
    padding: 15px;
    background-color: #fff;
    .data_my {
      width: 100%;
      height: 80px;
      margin-top: 15px;
      border: 2px solid #f6aea7;
      border-radius: 8px;
      background: url()
        no-repeat;
      background-size: 100% 100%;
      background-color: #fff9f9;
      position: relative;
      display: flex;
      align-items: center;
      .my_info {
        height: 48px;
        margin-left: 15px;
        display: flex;
        .photo {
          width: 48px;
          height: 48px;
          border-radius: 50%;
          overflow: hidden;
          position: relative;
          .van-uploader {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            z-index: 1;
          }
          // input[type="file"] {
          //   width: 100%;
          //   height: 100%;
          //   opacity: 0;
          //   position: absolute;
          //   top: 0;
          //   left: 0;
          //   z-index: 1;
          // }
          img {
            width: 100%;
            height: 100%;
            position: absolute;
            top: 0;
            left: 0;
          }
        }
        .info_name {
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          font-size: 14px;
          margin-left: 10px;
          span {
            &:nth-child(2) {
              font-size: 12px;
              color: #999999;
              margin-top: 8px;
            }
          }
        }
      }
      .current {
        position: absolute;
        top: 0;
        right: 0;
        background-color: #fde5e4;
        color: #fc4141;
        font-size: 14px;
        padding: 3px 8px;
        border-radius: 0 8px 0 10px;
      }
    }
  }
  .data_form {
    margin-top: 10px;
    padding: 0 15px;
    background-color: #fff;
    .van-cell {
      padding-left: 0;
      padding-right: 0;
      font-size: 16px;
      input {
        color: #868686;
      }
    }
  }
  .login_operate {
    width: 100%;
    padding: 0 15px;
    margin-top: 30px;
    button {
      border-radius: 20px;
      margin-bottom: 15px;
      &:nth-child(2) {
        background-color: transparent;
        border: 1px solid #fb8362;
        color: #ff3700;
      }
    }
  }
}
</style>